package com.javaee.ch12.web.api;

import com.javaee.ch12.common.response.ResultVo;
import com.javaee.ch12.entity.Book;
import com.javaee.ch12.service.BookService;
import com.javaee.ch12.web.dto.BookDto;
import com.javaee.ch12.web.dto.UserDto;
import com.javaee.ch12.web.dto.queryDto.BookQueryDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/book")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 添加书籍
     */

    @PostMapping("/")
    public ResultVo add(@RequestBody BookDto bookDto) {
        bookService.addBook(bookDto);
        return ResultVo.ok();
    }

    /**
     * 编辑书籍
     */

    @PutMapping("/")
    public ResultVo update(@RequestBody  BookDto bookDto) {
        bookService.updateBook(bookDto);
        return ResultVo.ok();
    }

    /**
     * 删除书籍
     */

    @DeleteMapping("/{bookId}")
    public ResultVo delete(@PathVariable Long bookId) {
        return bookService.removeById(bookId)
                ? ResultVo.ok("删除成功")
                : ResultVo.fail("数据不存在,删除失败");
    }


    /**
     * 条件查询书籍（GET 方式）
     */
    @GetMapping
    public ResultVo<List<Book>> queryBooksByGet(BookQueryDto queryDto) {
        return ResultVo.ok(bookService.query(queryDto));
    }

    /**
     * 条件查询书籍（POST 方式）
     */
    @PostMapping("/query")
    public ResultVo<List<Book>> queryBooksByPost(@RequestBody BookQueryDto queryDto) {
        return ResultVo.ok(bookService.query(queryDto));
    }
}